<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * 关键词
 * @author 66diqiu-hdk
 *
 */
class Admin_Source_module extends CI_Module {
	private $sid;
	/**
	 *
	 * 构造函数
	 */
	public function __construct(){
		parent::__construct();
		$this->sid = $GLOBALS['sid'];
		$this->load->model('admin.Source_model');
	}

	/**
	 *
	 * 关键词
	 */
	function index(){
		$sort = $this->input->get_post('sort');
		$condition = array();
		if($sort)$condition['sort'] = $sort;
//		$result = $this->Source_model->get_list(array('sid'=>$this->sid));
		$page_config['perpage']=20;   //每页条数
		$page_config['part']=2;//当前页前后链接数量
		$page_config['url']='admin/source/index';//url
		$page_config['param'] = '?sort='.$condition['sort'];//参数
		$page_config['seg']=4;//参数取 index.php之后的段数，默认为3，即index.php/control/function/18 这种形式
		$page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
		$page_config['total'] = $this->Source_model->get_count($condition);
		$this->load->library('mypageclass');
		$this->mypageclass->initialize($page_config);
		$result = $this->Source_model->get_list($page_config['perpage'],$page_config['nowindex'],$condition);
		foreach($result as $key=>$val){
			$result[$key]['edit_url'] = site_url('admin/source/edit/'.$val['id']);
			$result[$key]['remove_url'] = site_url('admin/source/remove/'.$val['id']);
			switch ($val['sort']){
				case 1:$result[$key]['sort_name'] = '票务';break;
				case 2:$result[$key]['sort_name'] = '保险';break;
				case 3:$result[$key]['sort_name'] = '地接';break;
				case 4:$result[$key]['sort_name'] = '领馆';break;
				case 5:$result[$key]['sort_name'] = '领队';break;
				case 6:$result[$key]['sort_name'] = '导游';break;
				case 7:$result[$key]['sort_name'] = '购物';break;
				case 8:$result[$key]['sort_name'] = '餐馆';break;
				case 9:$result[$key]['sort_name'] = '酒店';break;
				case 10:$result[$key]['sort_name'] = '景区';break;
				case 11:$result[$key]['sort_name'] = '车辆';break;
				case 12:$result[$key]['sort_name'] = '其他';break;
				default:$result[$key]['sort_name'] = '其他';break;
			}
		}
		$data['condition'] = $condition;
		$data['list'] = $result;
		$data['add_url'] = site_url('admin/source/add');   
        $data['index_url'] = site_url('admin/source/index');
		$this->load->view('source/index',$data);
	}
	/**
	 *
	 * 关键词的修改
	 */
	function add(){
		$data['form_post'] = site_url('admin/source/save');
		$data['check_name'] = site_url("admin/source/check_name?id=0");
        $data['add_url'] = site_url('admin/source/add');   
        $data['index_url'] = site_url('admin/source/index');
		$this->load->view('source/add',$data);
	}
	/**
	 *
	 * 关键词的修改
	 */
	function edit($id){
		$row = $this->Source_model->get_row(array('id'=>$id));
		$data['row'] = $row;
		//数据保存URL
		$data['form_post'] = site_url('admin/source/save');
		$data['check_name'] = site_url("admin/source/check_name?id=$id");
		$data['index_url'] = site_url('admin/source/index');
		$this->load->view('source/edit',$data);
	}
	/**
	 *
	 * 删除操作
	 */
	function remove($id){
		if(empty($id)){
			$post_id = $this->input->post('id');
			$res = $this->Source_model->remove_row("id in ($post_id)");
		}else{
			$res = $this->Source_model->remove_row(array('id'=>$id));
		}
		if($res){
			$this->success('操作成功',site_url('admin/source/index'),true);
		}else{
			$this->error('操作失败','',true);
		}
	}

	/**
	 * 提交保存关键词
	 */
	function save(){
		$data = $this->input->post('Form');
		$data['sid'] = $GLOBALS['sid'];
		$id = $this->input->post('id');
		if(empty($id)){
			//操作日志
			$log = array('action' => 'add');
			$data['update_person'] = $this->session->userdata('admin_user_name');
			$data['update_date'] = date('Y-m-d H:i:s',now());
			$res = $this->Source_model->add_row($data);
			$log = array('action'=>'add','log'=>'项目资源('.$data['name'].')');
		}else{
			//操作日志
			//			$log = array('action' => 'edit');
			$data['update_person'] = $this->session->userdata('admin_user_name');
			$data['update_date'] = date('Y-m-d H:i:s',now());
			$res = $this->Source_model->edit_row($data,array('id'=>$id));
			$log = array('action'=>'edit','log'=>'项目资源('.$data['name'].')');
		}
		if($res){
			//操作日志添加
			$this->_record_log($log);
			$this->success('操作成功',site_url('admin/source/index'),true);
		}else{
			$this->error('操作失败',site_url('admin/source/index'),true);
		}
	}
	/**
	 *
	 * 判断名称是否存在
	 */
	function check_name(){
		$arg_get = $this->input->get();
		$row = $this->Source_model->get_row(array('name'=>$arg_get['Form']['name'],'id !='=>$arg_get['id']));
		if(!empty($row) && is_array($row)){
			$this->ajaxReturn('词库名称已存在!');
		}else{
			$this->ajaxReturn(true);
		}
	}


}